var imgId = "";
function addAttach() {
	$
			.actions({
				actions : [
						{
							text : "选择图片",
							onClick : function() {
								pickImage(function(imgIds) {
									imgId = imgIds[0];
									var viewImgId = imgId.substring(imgId
											.lastIndexOf("/") + 1);
									$("#img_container").empty();
									$("#img_container")
											.append(
													"<a>"
															+ viewImgId
															+ "</a><i class='weui_icon_clear' onclick='removeImage()'></i>");
								});
							}
						}, {
							text : "选择文件",
							onClick : function() {
								$("#attach_file").click();
							}
						}, {
							text : "文件库",
							onClick : function() {
								$("#file_list").popup();
							}
						} ]
			});
}

function onChange() {
	$("#file_container").empty();
	var attach = $("#attach_file").val();
	if (typeof (attach) == "undefined" || attach == "") {

	} else {
		attach = attach.substring(attach.lastIndexOf("\\") + 1);
		$("#file_container")
				.append(
						"<a>"
								+ attach
								+ "</a><i class='weui_icon_clear' onclick='removeAttach()'></i>");
	}
}

function removeAttach() {
	$("#attach_file").val("");
	onChange();
}

function removeImage() {
	$("#img_container").empty();
	imgId = "";
}

function pickImage(onSuccess) {
	wx.chooseImage({
		count : 1, // 默认9
		sizeType : [ 'compressed' ], // 可以指定是原图还是压缩图，默认二者都有
		sourceType : [ 'album', 'camera' ], // 可以指定来源是相册还是相机，默认二者都有
		success : function(res) {
			onSuccess(res.localIds);
		}
	});
}

var imgArray;
function ImageUploader(imgIds, callback, onfail) {
	this.imgIds = imgIds;
	this.callback = callback;
	this.serverIds = "";
	this.currIndex = 0;
	this.uploader = this;
	this.upload = function() {
		wx.uploadImage({
			localId : this.imgIds[currIndex], // 需要上传的图片的本地ID，由chooseImage接口获得
			isShowProgressTips : 0, // 默认为1，显示进度提示
			success : function(res) {
				var serverId = res.serverId; // 返回图片的服务器端ID
				serverIds = serverIds + serverId + ",";
				currIndex = currIndex + 1;
				if (currIndex >= imgIds.length) {
					uploader.callback(serverIds);
				} else {
					uploader.upload();
				}
			},
			fail : function(res) {
				onfail();
			}
		});

	}

}

var serverIds = "";
var currIndex = 0;
function uploadImgs() {

}

function checkFile(a){
	var item = $(a).find(".file_item");
	if(item.hasClass("active")){
		item.removeClass("active");
	}else{
		item.addClass("active");
	}
}

function closePopup(){
	$("#wxfile_view_container").empty();
	
	$(".file_item.active").each(function(){
		var index = $(this).parent().index();
		$("#wxfile_view_container").append("<div class='attach_item h_align_center'><a>"
															+ wxFileData[index].name
															+ "</a><i class='weui_icon_clear' onclick='removeWxFile(this,"+wxFileData[index].id+")'></i></div>");
	});
	$(".weui-popup-container").hide();
	console.log($(".file_item.active").attr("id"));
}

function removeWxFile(i,id){
	$(i).parent().remove();
	$("#wx_"+id).removeClass("active");
}